﻿Imports SIGI.Common.BE

Public Class frmAppointmentViewer

#Region "<< -Enums- >>"
    Private Enum AppointmentPreviewResult
        None
        Discard
    End Enum

    Public Enum Mode
        Preview
        Warning
    End Enum
#End Region

#Region "<< -Attributes- >>"
    Private _Behavior As Mode
    Private _Appointment As AppointmentBE = Nothing
    Private _AppointmentResultPreview As AppointmentPreviewResult = AppointmentPreviewResult.None
#End Region

#Region "<< -Properties- >>"

#End Region

#Region "<< -Events- >>"
    Private Sub frmAppointmentPreview_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        Dim wTmp As Integer
        Dim wStartHour As String = String.Empty
        Dim wEndHour As String = String.Empty
        Dim wTime As String = String.Empty

        If _Appointment Is Nothing Then Return
        wStartHour = Format(_Appointment.DateStart, "Short Time")
        wEndHour = Format(_Appointment.DateEnd, "Short Time")

        lblSubject.Text = String.Format("({0} - {1}) {2}", wStartHour, wEndHour, _Appointment.Subject)
        txtDescription.Text = _Appointment.Description

        wTmp = DateDiff(DateInterval.Minute, Date.Now, _Appointment.DateStart.Value)
        If wTmp <= 0 Then
            cmbWarning.Items.Add(String.Format("{0} Minutos", wTmp))
            cmbWarning.SelectedIndex = cmbWarning.Items.Count - 1
            cmbWarning.Enabled = False
            If wTmp >= 60 Then '1 Hora
                'Descarta la cita automáticamente
                _AppointmentResultPreview = AppointmentPreviewResult.Discard
            End If
        Else
            wTime = IIf(_Appointment.Warning < 60, String.Format("{0} Minutos", _Appointment.Warning), "1 Hora")
            cmbWarning.SelectedIndex = cmbWarning.FindString(wTime)
        End If

    End Sub

    Private Sub btnDiscard_Click(sender As System.Object, e As System.EventArgs) Handles btnDiscard.Click

        If _MsgBox.ShowMessage("¿Descartar Cita?", "¿Esta seguro de que desea descartar la Cita?",
                               frmMessage.MessageType.Question_,
                               frmMessage.MessageButtons.OkCancel) = frmMessage.MsgResult.Cancel Then
            _AppointmentResultPreview = AppointmentPreviewResult.None
        Else
            _AppointmentResultPreview = AppointmentPreviewResult.Discard
            Me.Close()
        End If
    End Sub

    Private Sub frmAppointmentViewer_FormClosing(sender As System.Object, e As System.Windows.Forms.FormClosingEventArgs) Handles MyBase.FormClosing
        Dim wIdx As Integer = cmbWarning.Text.IndexOf(" ")
        Dim wNewWarning As Integer = Convert.ToInt32(cmbWarning.Text.Substring(0, wIdx))
        Dim wAptController As AppointmentController = Nothing

        If _AppointmentResultPreview <> AppointmentPreviewResult.Discard Then
            If _Appointment.Warning <> wNewWarning Then
                wAptController = New AppointmentController
                _Appointment.Warning = wNewWarning
                wAptController.Update(_Appointment)
            End If
        End If
    End Sub
#End Region

#Region "<< -Methods- >>"
    Public Sub New(ByVal pAppointment As AppointmentBE, ByVal pMode As Mode)
        InitializeComponent()
        _Appointment = pAppointment
        _Behavior = pMode
    End Sub

    Public Function IsDiscarded() As Boolean
        Return _AppointmentResultPreview = AppointmentPreviewResult.Discard
    End Function

    'Private Sub Config_CmbWarning()
    '    Dim wTime As String = String.Empty

    '    wTime = IIf(_Appointment.Warning < 60, String.Format("{0} Minutos", _Appointment.Warning), "1 Hora")
    '    cmbWarning.SelectedIndex = cmbWarning.FindString(wTime)
    '    'Select Case _Behavior
    '    '    Case Mode.Preview
    '    '        cmbWarning.SelectedIndex = cmbWarning.FindString(wTime)
    '    '        'cmbWarning.Enabled = False
    '    '    Case Mode.Warning
    '    '        cmbWarning.SelectedIndex = cmbWarning.FindString(wTime)
    '    '    Case Else

    '    'End Select
    'End Sub
#End Region


End Class
